#!/usr/bin/env bash

MODULE_NAME="all"
if [ -n "$1" ];then
  echo $1
  MODULE_NAME=$1
fi

CURRENT_DIR=$(dirname $PWD)
KEYSTONE_INTERFACE_DIR=$CURRENT_DIR"/swagger"
INTERFACE_OUT_DIR=$CURRENT_DIR"/baseinterface"

docker run -it --rm \
       -v $KEYSTONE_INTERFACE_DIR:/local \
       -v $INTERFACE_OUT_DIR:/out \
       -w /local \
       2539983484/swagger-python-codegen:v2 /out/gen_http_api.sh $MODULE_NAME

# modify the init files
add_source_path(){
  # $1 target __init__.py

  echo "import sys" >> $1
  echo "import os" >> $1
  echo 'if os.environ.get("scripts_sys_path") and os.environ.get("scripts_sys_path") not in sys.path:' >> $1
  echo '    sys.path.append(os.environ.get("scripts_sys_path"))' >> $1
}
#
HTTP_DIR=$INTERFACE_OUT_DIR"/http_client"
rm -rf $HTTP_DIR"/__init__.py"
#
#
SWAGGER_DIR=$HTTP_DIR"/swagger_client"
rm -rf $SWAGGER_DIR"/__init__.py"
mkinit $SWAGGER_DIR -w --noall
#
##add_source_path $SWAGGER_DIR"/__init__.py"
#
for file in `ls $SWAGGER_DIR`
do
  if [ -d $SWAGGER_DIR"/"$file ];then
##    rm -rf $SWAGGER_DIR"/"$file/__init__.py"
    mkinit $SWAGGER_DIR"/"$file -i --noall
  fi
done
#
touch $HTTP_DIR"/__init__.py"
add_source_path $HTTP_DIR"/__init__.py"

# 替换csv到multi, 支持多参数的形式。
SRC="= 'csv'"; DST="= 'multi'"
#echo `grep "$SRC" -rl http_client/swagger_client/`
echo `grep "$SRC" -rl $SWAGGER_DIR`
sed -i "" "s/$SRC/$DST/g" `grep "$SRC" -rl $SWAGGER_DIR`
